<?php
/*
*前台用户模块
*/
class User_model extends CI_Model{
	public function __construct(){
		$this->load->database();
		include APPPATH."config/ucenter.php";
		//include APPPATH."../uc_client/client.php";
		
	}

	//用户信息 , 角色信息 及其他
	public function userInfo($data){
		$this->db->select('id,openid,username,email,role_id,status');
		if($query = $this->db->get_where('user',$data)){
			return $query->row_array();
		}
		else{
			return false;
		}
	}
	
	/**
	* 列出所用用户
	* $s:start $l: limit
	*/
	public function userList($s = 0, $l = 10){
		$this->db->from('user');
		$this->db->limit($s,$l);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	//添加用户
	//$name,$password,$email,$role_id
	public function addUser($data){
		
		return $this->db->insert('user',$data);
	}
	
	//编辑用户
	public function editUser($data,$where){
		$this->db->where($where);
		return $this->db->update('user',$data);
	}
	
	//更新状态
	public function up_status($id,$status){
		
		$ids = explode(',',$id);
		$this->db->where_in('id',$ids);
		$this->db->update('user',$status);
		//Q: how to get the real affected_rows and the object
		//怎么获得受影响的行数以及具体行的信息
		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	
	//删除用户
	public function deleteUser($id){
		
		//删除
		$ids = explode(',',$id);
		$this->db->where_in('id',$ids);
		$this->db->delete('user');
		$rows = $this->db->affected_rows();
		if($rows >= 1){
			return true;
		}else{
			return false;
		}

	}
	
	//用户概况,统计
	public function userCount(){
		$query = $this->db->get('user');
		return $query->result_array();
	}
	
	//同步用户到uc
	public function user2uc(){

		
		$UCDB = mysql_connect(UC_DBHOST,UC_DBUSER,UC_DBPW);
		mysql_select_db(UC_DBNAME,$UCDB);
		//ucenter中最大的uid
		$query = mysql_query("SHOW CREATE TABLE ".UC_DBTABLEPRE."members");
	
		$data = mysql_fetch_array($query);

		$data = $data['Create Table'];
		if(preg_match('/AUTO_INCREMENT=(\d+?)[\s|$]/i', $data, $a)) {
			$maxuid = $a[1] - 1;
		} else {
			$maxuid = 0;
		}


		$query = $this->db->get("user");//查询所有用户
		$users = $query->result_array();
		foreach($users as $data) {
			$salt = rand(100000, 999999);
			$password = $data['password'];
			
			$data['realid'] = $data['id'];
			$lastuid = $data['id'] += $maxuid;
			$queryuc = mysql_query("SELECT count(*) FROM ".UC_DBTABLEPRE."members WHERE username='$data[username]'",$UCDB);
			
			$userexist = count($queryuc['count(*)']);
			if(!$userexist) {
				mysql_query("INSERT LOW_PRIORITY INTO ".UC_DBTABLEPRE."members SET uid='$data[id]', username='$data[username]', password='$password',
					email='$data[email]', regip='', regdate='', salt='$salt'");
				mysql_query("INSERT LOW_PRIORITY INTO ".UC_DBTABLEPRE."memberfields SET uid='$data[id]'");
				
				//同步更新的状态
				$this->db->where(array('id'=>$data['realid']));
				$this->db->update('user',array('openid'=>$data['realid'],'uc_connect'=>1));
				
			} else {
				mysql_query("REPLACE INTO ".UC_DBTABLEPRE."mergemembers SET appid='".UC_APPID."', username='$data[username]'");
			}
		
			
			
		}

		mysql_query("ALTER TABLE ".UC_DBTABLEPRE."members AUTO_INCREMENT=".($lastuid + 1));

	}
	

}	
?>